#include "irm.h"

namespace ophies {


RequestCallback::RequestCallback(DataModelType type, QObject *parent) :
    m_type(type),
    QObject(parent)
{

}


void RequestCallback::onFinished(QNetworkReply *r)
{
    //    std::cout << r->readAll();
    //    std::cout << "RequestCallback::onFinished called 11";

    std::cout << "RequestCallback::onFinished called 11" << std::endl;
	handlResponse(r);
	r->deleteLater();

}

void RequestCallback::onAuthenticationRequired(QNetworkReply*, QAuthenticator*)
{
    std::cout << "RequestCallback::onAuthenticationRequired called" << std::endl;
}


void RequestCallback::onListReceived(QList<DataModel*>* list)
{
	if(list) 
		std::cout << "object is not null but ... " << std::endl;

	std::cout << "onListReceived is not implemented" << " class type: " << m_type << std::endl;
}

void RequestCallback::onObjectReceived(DataModel* object)
{
	if(object)
		std::cout << "object is not null but ... " << std::endl;
    
	std::cout << "onObjectReceived is not implemented" << " class type: " << m_type << std::endl;
}

void RequestCallback::handlResponse( QNetworkReply *r )
{
	QList<DataModel*> *list = NULL;
	DataModel* object = NULL;

	QString jsonStr = QString(r->readAll());

	if(jsonStr.startsWith('[')) // json array
	{

		list = Helper::listFromJsonStr(m_type, jsonStr, parent());
		onListReceived(list);

	} else if(jsonStr.startsWith('{')) // json object
	{

		object = Helper::objectFromJsonStr(m_type, jsonStr, parent());
		onObjectReceived(object);

	} else {

	}
}

RequestCallback::~RequestCallback()
{

}

} // namespace ophies


/////////////////////////////////////////////////////////////////
/// \brief PatientsCallback::PatientsCallback
/// \param parent
////////////////////////////////////////////////////////////////
PatientsCallback::PatientsCallback(RequestCallback *parent) :
    RequestCallback(ophies::PATIENT, parent)
{

}

PatientsCallback::~PatientsCallback()
{

}





/////////////////////////////////////////////////////////////////
/// \brief DcmStudiesCallback::DcmStudiesCallback
/// \param parent
/////////////////////////////////////////////////////////////////
DcmStudiesCallback::DcmStudiesCallback(RequestCallback *parent) :
    RequestCallback(ophies::DCM_STUDY, parent)
{
}

DcmStudiesCallback::~DcmStudiesCallback()
{

}



////////////////////////////////////////////////////////////////
/// \brief DcmSeriesCallback::DcmSeriesCallback
/// \param parent
///////////////////////////////////////////////////////////////
DcmSeriesCallback::DcmSeriesCallback(RequestCallback *parent) :
    RequestCallback(ophies::DCM_SERIES, parent)
{
}

DcmSeriesCallback::~DcmSeriesCallback()
{

}





//////////////////////////////////////////////////////////////
/// \brief DcmInstancesCallback::DcmInstancesCallback
/// \param parent
//////////////////////////////////////////////////////////////
DcmInstancesCallback::DcmInstancesCallback(RequestCallback *parent) :
    RequestCallback(ophies::DCM_INSTANCE, parent)
{
}

DcmInstancesCallback::~DcmInstancesCallback()
{

}





//////////////////////////////////////////////////////////////
/// \brief DocsetCallback::DocsetCallback
/// \param parent
//////////////////////////////////////////////////////////////
DocsetsCallback::DocsetsCallback(RequestCallback *parent) :
    RequestCallback(ophies::DOCSET, parent)
{
}

DocsetsCallback::~DocsetsCallback()
{

}



